package com.adcc.loadsheet.billing.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加输入参数校验过滤器
 * Created by zhaoml on 2020-04-01.
 */
public class ParameterCheckFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ParameterCheckServletRequestWrapper myWrapper = new ParameterCheckServletRequestWrapper((HttpServletRequest) servletRequest);
        if (myWrapper.isHasError()) {
            String headerValue = ((HttpServletRequest) servletRequest).getHeader("accept");
            if (headerValue != null && headerValue.indexOf("application/json") > -1) {
                ((HttpServletResponse) servletResponse).sendRedirect("/errorpage/errorParamVO");
            } else {
                ((HttpServletResponse) servletResponse).sendRedirect("/errorpage/errorParam");
            }
        } else {
            filterChain.doFilter(myWrapper, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}